package com.simple.portal.bo.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.simple.portal.bo.IRightInfoBO;
import com.simple.portal.dao.IGroupRightRelDAO;
import com.simple.portal.dao.IRightInfoDAO;
import com.simple.portal.vo.GroupRightRelVO;
import com.simple.portal.vo.RightInfoVO;
import com.simple.util.RightInfoLoad;
import com.simple.util.common.Constants;
import com.simple.util.object.portal.AuthorizationRightBean;

@Service("rightInfoBO")
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public class RightInfoBOImpl implements IRightInfoBO{

	private List<RightInfoVO> allRightInfoVOList;
	
	@Autowired
	private IRightInfoDAO rightInfoDAO;
	
	@Autowired
	private IGroupRightRelDAO groupRightRelDAO;
	
	
	@Override
	public RightInfoVO findById(String id) {
		return rightInfoDAO.findById(id);
	}


	@Override
	public List<RightInfoVO> findAll() {
		return rightInfoDAO.findAll();
	}
	

	@Override
	public List<RightInfoVO> getAllRightInfoVOList(){
		if(allRightInfoVOList==null){
			allRightInfoVOList=findAll();
		}
		return allRightInfoVOList;
	}
	
	@Override
	public List<RightInfoVO> getAllByGroupIdAndRightFlag(String groupId,String rightFlag){
		
		List <GroupRightRelVO> groupRightRelVOList=groupRightRelDAO.findAllByGroupIdAndRightFlag(groupId, rightFlag);
		
		List<RightInfoVO> rightInfoVOList= getAllRightInfoVOList();
		
		List<RightInfoVO> tempRightInfoVOList=new ArrayList<RightInfoVO>();
		
		for(GroupRightRelVO groupRightRelVO: groupRightRelVOList){
			
			for(RightInfoVO rightInfoVO : rightInfoVOList){
				
				if(rightInfoVO.getRightId().equals(groupRightRelVO.getRightId()) ){
					
					tempRightInfoVOList.add(rightInfoVO);
					
					break;
				}
				
			}
			
		}
		 return tempRightInfoVOList;
	}
	
	@Override
	public List<AuthorizationRightBean> getAuthorizationRightBeanList(final List<RightInfoVO> rightInfoVOListA, final String groupId){
		final List<AuthorizationRightBean> authorizationRightBeanList = new ArrayList<AuthorizationRightBean>();
		final List<RightInfoVO> allRightInfoVOList = rightInfoDAO.findAll();
		final List<RightInfoVO> rightInfoVOListOTemp = RightInfoLoad.getInstance().getAllByGroupIdAndRightFlag(groupId, Constants.GROUP_RIGHT_FLAG_OPERATION);
		final List<RightInfoVO> rightInfoVOListATemp = RightInfoLoad.getInstance().getAllByGroupIdAndRightFlag(groupId, Constants.GROUP_RIGHT_FLAG_AUTHORIZATION);
		
		AuthorizationRightBean authorizationRightBean;
		for(RightInfoVO allRightInfoVO : allRightInfoVOList){
			authorizationRightBean = new AuthorizationRightBean();
			authorizationRightBean.setRightInfoVO(allRightInfoVO);
			
			for(RightInfoVO rightInfoVO : rightInfoVOListA){
				if(allRightInfoVO.getRightId().equals(rightInfoVO.getRightId())){
					authorizationRightBean.setNoRightBoolean(false);
					break;
				}
			}
			authorizationRightBeanList.add(authorizationRightBean);
		}
		
			
		
		for(AuthorizationRightBean arb : authorizationRightBeanList){
			for(RightInfoVO rightInfoVOOTemp : rightInfoVOListOTemp){
				if(arb.getRightInfoVO().getRightId().equals(rightInfoVOOTemp.getRightId())){
					arb.setOperationBoolean(true);
					break;
				}
			}
			
			for(RightInfoVO rightInfoVOATemp : rightInfoVOListATemp){
				if(arb.getRightInfoVO().getRightId().equals(rightInfoVOATemp.getRightId())){
					arb.setAuthorizationBoolean(true);
					break;
				}
			}
			
			
		}
		
		return authorizationRightBeanList;
	}
	
}
